package jrdcom.com.jrdmovie.Fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import jrdcom.com.jrdmovie.Glide.JrdImageLoaderProxy;
import jrdcom.com.jrdmovie.JrdHttpMethod.Beans.JrdMoveDetailBean;
import jrdcom.com.jrdmovie.JrdHttpMethod.Beans.JrdSubjectBean;
import jrdcom.com.jrdmovie.JrdHttpMethod.JrdHttpMethod;
import jrdcom.com.jrdmovie.JrdHttpMethod.JrdSubscriber.JrdOnNextListener;
import jrdcom.com.jrdmovie.JrdHttpMethod.JrdSubscriber.ProgressSubscriber;
import jrdcom.com.jrdmovie.Present.JrdMovieDetailConstract;
import jrdcom.com.jrdmovie.Present.JrdMovieDetailPresent;
import jrdcom.com.jrdmovie.R;

/**
 * Created by longcheng on 2017/3/28.
 */

public class JrdMoveDetailFragment extends Fragment implements JrdMovieDetailConstract.JrdMovieView{

    @Bind(R.id.movie_detail_SDV)
    ImageView movieDetailSDV;
    @Bind(R.id.movie_detail_title_TV)
    TextView movieDetailTitleTV;
    @Bind(R.id.movie_detail_director_TV)
    TextView movieDetailDirectorTV;
    @Bind(R.id.movie_detail_cast_TV)
    TextView movieDetailCastTV;
    @Bind(R.id.movie_detail_original_TV)
    TextView movieDetailOriginalTV;
    @Bind(R.id.movie_detail_year_TV)
    TextView movieDetailYearTV;
    @Bind(R.id.movie_detail_genres_TV)
    TextView movieDetailGenresTV;
    @Bind(R.id.movie_detail_countries_TV)
    TextView movieDetailCountriesTV;
    @Bind(R.id.movie_detail_summary_TV)
    TextView movieDetailSummaryTV;
    @Bind(R.id.fragment_movie_detail_SV)
    ScrollView fragmentMovieDetailSV;

    public static final String TAG = JrdMoveDetailFragment.class.getSimpleName();//this.getClass().getSimpleName();
    private String JrdMovieId;
    private JrdMovieDetailConstract.JrdMoviePresent moviePresent;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        moviePresent = new JrdMovieDetailPresent(getContext());
        moviePresent.setView(this);
    }

    private void getMovieId(){
        Bundle bundle = getArguments();
        JrdMovieId = bundle.getString("MovieId");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_movie_detail, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getMovieId();
        getMovieDetail();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    private void getMovieDetail(){
        moviePresent.getMovieDetail(JrdMovieId);
    }

    @Override
    public void updateScreen(JrdMoveDetailBean moveDetailBean) {
        JrdImageLoaderProxy.getInstance().displayImage(moveDetailBean.getImages().getMedium(),movieDetailSDV, getContext());
        /*
        *     @Bind(R.id.movie_detail_title_TV)
    TextView movieDetailTitleTV;
    @Bind(R.id.movie_detail_director_TV)
    TextView movieDetailDirectorTV;
    @Bind(R.id.movie_detail_cast_TV)
    TextView movieDetailCastTV;
    @Bind(R.id.movie_detail_original_TV)
    TextView movieDetailOriginalTV;
    @Bind(R.id.movie_detail_year_TV)
    TextView movieDetailYearTV;
    @Bind(R.id.movie_detail_genres_TV)
    TextView movieDetailGenresTV;
    @Bind(R.id.movie_detail_countries_TV)
    TextView movieDetailCountriesTV;
    @Bind(R.id.movie_detail_summary_TV)
    TextView movieDetailSummaryTV;
    @Bind(R.id.fragment_movie_detail_SV)
    ScrollView fragmentMovieDetailSV;
        * */
        movieDetailTitleTV.setText(moveDetailBean.getTitle());
        movieDetailYearTV.setText("年代"+moveDetailBean.getYear());
        movieDetailCountriesTV.setText(moveDetailBean.getCountries().toString());
        movieDetailDirectorTV.setText(getCastNames(moveDetailBean.getCasts()));
        movieDetailDirectorTV.setText(getDirNames(moveDetailBean.getDirectors()));
        movieDetailSummaryTV.setText(moveDetailBean.getSummary());
        movieDetailOriginalTV.setText(moveDetailBean.getOriginal_title());
        movieDetailGenresTV.setText(moveDetailBean.getGenres().toString());
    }

    public static String getCastNames(List<JrdMoveDetailBean.CastsBean> castArray){
        StringBuilder sb = new StringBuilder();
        for (JrdMoveDetailBean.CastsBean cast : castArray) {
            sb.append(cast.getName() + "/");
        }

        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
        }
        return sb.toString();
    }

    public static String getDirNames(List<JrdMoveDetailBean.DirectorsBean> directorsBeen){
        StringBuilder sb = new StringBuilder();
        for (JrdMoveDetailBean.DirectorsBean cast : directorsBeen) {
            sb.append(cast.getName() + "/");
        }

        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
        }
        return sb.toString();
    }
}
